/*
 * Class: CustomerAdapter
 * Created by: Mary Joy Pascual
 * Date Created: 06/16/2014
 * Company: Netsuite
*/
package com.netsuite.adapter;

import com.netsuite.constants.Constants;
import com.netsuite.logger.LoggerImp;
import com.netsuite.pojo.CustomerPOJO;
import com.netsuite.webservices.lists.relationships_2014_1.Customer;
import com.netsuite.webservices.platform.core_2014_1.RecordRef;

public class CustomerAdapter {
	
	private static String CLASS_NAME = CustomerAdapter.class.getName();

	public static Customer setCustomer(CustomerPOJO customerPojo){
		
		LoggerImp.startLog(CLASS_NAME, "setCustomer() - CustomerPOJO to Customer");
		
		Customer customer = new Customer();
		customer.setIsPerson(customerPojo.getIsPerson());
		customer.setEntityId(customerPojo.getCompanyName());
		customer.setCompanyName(customerPojo.getCompanyName());
		customer.setFirstName(customerPojo.getFirstName());
		customer.setLastName(customer.getLastName());
		customer.setMiddleName(customerPojo.getMiddleName());
		
		if(customerPojo.getInternalId() != null){
			customer.setInternalId(customerPojo.getInternalId());
		}
		if(customerPojo.getExternalId() != null){
			customer.setExternalId(customerPojo.getExternalId());
		}
		
		RecordRef customFormRef = new RecordRef();
		customFormRef.setInternalId(Constants.DEFAULT_FORM);
		customer.setCustomForm(customFormRef);

		RecordRef subRef = new RecordRef();
  		subRef.setInternalId(customerPojo.getSubsidiary());
  		customer.setSubsidiary(subRef);

  		RecordRef status = new RecordRef();
  		status.setInternalId(customerPojo.getStatus());
  		customer.setEntityStatus(status);
  		
  		LoggerImp.endLog(CLASS_NAME, "setCustomer() - CustomerPOJO to Customer");
  		
  		return customer;
	}
	
	public static CustomerPOJO setCustomer(Customer customer){
		
		LoggerImp.startLog(CLASS_NAME, "setCustomer() - Customer to CustomerPOJO");
		
		CustomerPOJO customerPojo = new CustomerPOJO();
		customerPojo.setIsPerson(customer.getIsPerson());
		customerPojo.setCompanyName(customer.getCompanyName());
		customerPojo.setFirstName(customer.getFirstName());
		customerPojo.setLastName(customer.getLastName());
		customerPojo.setMiddleName(customer.getMiddleName());
		customerPojo.setCustomerName(customer.getEntityId());
		customerPojo.setEntityId(customer.getEntityId());
		
		if(customer.getInternalId() != null){
			customerPojo.setInternalId(customer.getInternalId());
		}
		if(customer.getExternalId() != null){
			customerPojo.setExternalId(customer.getExternalId());
		}

		customerPojo.setSubsidiary(customer.getSubsidiary().getName());
  		customerPojo.setStatus(customer.getEntityStatus().getName());
  		
  		LoggerImp.endLog(CLASS_NAME, "setCustomer() - Customer to CustomerPOJO");
  		
  		return customerPojo;
	}
	
}
